<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  
  <title>对象属性类型问题 | 漠北烟云</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="description" content="这行代码会输出什么呢 1234567let a &#x3D; &amp;#123;&amp;#125;;let b &#x3D; &amp;#x27;123&amp;#x27;;let c &#x3D; 123;a[b] &#x3D; &amp;#x27;b&amp;#x27;;a[c] &#x3D; &amp;#x27;c&amp;#x27;;console.log(a[b]) 输出ckey 只能接收string和Symbol类型 因此 123 会被转化为‘123’，导致值被覆盖 1234567let a">
<meta property="og:type" content="article">
<meta property="og:title" content="对象属性类型问题">
<meta property="og:url" content="http://example.com/2023/03/06/%E5%AF%B9%E8%B1%A1%E5%B1%9E%E6%80%A7%E7%B1%BB%E5%9E%8B%E9%97%AE%E9%A2%98/index.html">
<meta property="og:site_name" content="漠北烟云">
<meta property="og:description" content="这行代码会输出什么呢 1234567let a &#x3D; &amp;#123;&amp;#125;;let b &#x3D; &amp;#x27;123&amp;#x27;;let c &#x3D; 123;a[b] &#x3D; &amp;#x27;b&amp;#x27;;a[c] &#x3D; &amp;#x27;c&amp;#x27;;console.log(a[b]) 输出ckey 只能接收string和Symbol类型 因此 123 会被转化为‘123’，导致值被覆盖 1234567let a">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2023-03-06T13:46:59.000Z">
<meta property="article:modified_time" content="2023-03-26T14:54:31.835Z">
<meta property="article:author" content="mobeiyanyun">
<meta property="article:tag" content="js基础">
<meta name="twitter:card" content="summary">
  
    <link rel="alternate" href="/atom.xml" title="漠北烟云" type="application/atom+xml">
  
  
    <link rel="shortcut icon" href="/favicon.png">
  
  
    
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/typeface-source-code-pro@0.0.71/index.min.css">

  
  
<link rel="stylesheet" href="/css/style.css">

  
    
<link rel="stylesheet" href="/fancybox/jquery.fancybox.min.css">

  
<meta name="generator" content="Hexo 6.3.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">漠北烟云</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/">Home</a>
        
          <a class="main-nav-link" href="/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://example.com"></form>
      </div>
    </div>
  </div>
</header>

      <div class="outer">
        <section id="main"><article id="post-对象属性类型问题" class="h-entry article article-type-post" itemprop="blogPost" itemscope itemtype="https://schema.org/BlogPosting">
  <div class="article-meta">
    <a href="/2023/03/06/%E5%AF%B9%E8%B1%A1%E5%B1%9E%E6%80%A7%E7%B1%BB%E5%9E%8B%E9%97%AE%E9%A2%98/" class="article-date">
  <time class="dt-published" datetime="2023-03-06T13:46:59.000Z" itemprop="datePublished">2023-03-06</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="p-name article-title" itemprop="headline name">
      对象属性类型问题
    </h1>
  

      </header>
    
    <div class="e-content article-entry" itemprop="articleBody">
      
        <p>这行代码会输出什么呢</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">let</span> a = &#123;&#125;;</span><br><span class="line"><span class="built_in">let</span> b = <span class="string">&#x27;123&#x27;</span>;</span><br><span class="line"><span class="built_in">let</span> c = 123;</span><br><span class="line">a[b] = <span class="string">&#x27;b&#x27;</span>;</span><br><span class="line">a[c] = <span class="string">&#x27;c&#x27;</span>;</span><br><span class="line">console.log(a[b])</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p>输出c<br>key 只能接收string和Symbol类型 因此 123 会被转化为‘123’，导致值被覆盖</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">let</span> a = &#123;&#125;;</span><br><span class="line"><span class="built_in">let</span> b = Symblo(<span class="string">&#x27;123&#x27;</span>);</span><br><span class="line"><span class="built_in">let</span> c = Symblo(<span class="string">&#x27;123&#x27;</span>);</span><br><span class="line">a[b] = <span class="string">&#x27;b&#x27;</span>;</span><br><span class="line">a[c] = <span class="string">&#x27;c&#x27;</span>;</span><br><span class="line">console.log(a[b])</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p>输出b</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">let</span> a = &#123;&#125;;</span><br><span class="line"><span class="built_in">let</span> b = &#123;key:<span class="string">&#x27;123&#x27;</span>&#125;;</span><br><span class="line"><span class="built_in">let</span> c = &#123;key:<span class="string">&#x27;456&#x27;</span>&#125;;</span><br><span class="line">a[b] = <span class="string">&#x27;b&#x27;</span>;</span><br><span class="line">a[c] = <span class="string">&#x27;c&#x27;</span>;</span><br><span class="line">console.log(a[b])</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p>输出 c</p>
<h4 id="js对象Key的数据类型"><a href="#js对象Key的数据类型" class="headerlink" title="js对象Key的数据类型"></a>js对象Key的数据类型</h4><ul>
<li>只能是字符串和Symbol类型</li>
<li>其他类型会被转换成字符串</li>
<li>转换会调用toString()方法</li>
</ul>
<h4 id="Map和WeakMap的Key"><a href="#Map和WeakMap的Key" class="headerlink" title="Map和WeakMap的Key"></a>Map和WeakMap的Key</h4><ul>
<li>map 的key 可以是各种类型</li>
<li>WeakMap的Key只能是引用类型，不能是值类型</li>
</ul>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://example.com/2023/03/06/%E5%AF%B9%E8%B1%A1%E5%B1%9E%E6%80%A7%E7%B1%BB%E5%9E%8B%E9%97%AE%E9%A2%98/" data-id="clfziy0oo0009p5r40uh292ec" data-title="对象属性类型问题" class="article-share-link">Share</a>
      
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/js%E5%9F%BA%E7%A1%80/" rel="tag">js基础</a></li></ul>

    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2023/03/07/for-in%20for-of/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          for-in for-of
        
      </div>
    </a>
  
  
    <a href="/2023/03/05/JSBridge/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">JSBridge实现原理</div>
    </a>
  
</nav>

  
</article>


</section>
        
          <aside id="sidebar">
  
    

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tags</h3>
    <div class="widget">
      <ul class="tag-list" itemprop="keywords"><li class="tag-list-item"><a class="tag-list-link" href="/tags/VUE/" rel="tag">VUE</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/js%E5%9F%BA%E7%A1%80/" rel="tag">js基础</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E5%B0%8F%E7%A8%8B%E5%BA%8F-uniapp/" rel="tag">小程序&#x2F;uniapp</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E6%B7%B7%E5%90%88%E5%BC%80%E5%8F%91/" rel="tag">混合开发</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tag Cloud</h3>
    <div class="widget tagcloud">
      <a href="/tags/VUE/" style="font-size: 15px;">VUE</a> <a href="/tags/js%E5%9F%BA%E7%A1%80/" style="font-size: 20px;">js基础</a> <a href="/tags/%E5%B0%8F%E7%A8%8B%E5%BA%8F-uniapp/" style="font-size: 10px;">小程序/uniapp</a> <a href="/tags/%E6%B7%B7%E5%90%88%E5%BC%80%E5%8F%91/" style="font-size: 10px;">混合开发</a>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2023/04/">April 2023</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2023/03/">March 2023</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2023/04/02/%E5%B0%8F%E7%A8%8B%E5%BA%8F%E7%99%BB%E5%BD%95%E8%AE%BE%E8%AE%A1/">小程序登录设计</a>
          </li>
        
          <li>
            <a href="/2023/03/30/%E8%A7%82%E5%AF%9F%E8%80%85%E6%A8%A1%E5%BC%8F/">vue中的观察者模式</a>
          </li>
        
          <li>
            <a href="/2023/03/29/%E6%95%B0%E6%8D%AE%E5%8F%98%E6%9B%B4%E7%9A%84%E7%9B%91%E6%B5%8B/">数据变更的监测</a>
          </li>
        
          <li>
            <a href="/2023/03/26/%E9%97%AD%E5%8C%85/">闭包</a>
          </li>
        
          <li>
            <a href="/2023/03/21/instanceof%E5%8E%9F%E7%90%86/">instanceof原理</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      
      &copy; 2023 mobeiyanyun<br>
      Powered by <a href="https://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>

    </div>
    <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    


<script src="/js/jquery-3.4.1.min.js"></script>



  
<script src="/fancybox/jquery.fancybox.min.js"></script>




<script src="/js/script.js"></script>





  </div>
</body>
</html>